package me.dwliu.lab.demo.codeview.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;

@Configuration
@EnableAuthorizationServer
public class CustomAuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        super.configure(security);
        security
                .tokenKeyAccess("isAuthenticated()")
                //允许check_token访问  permitAll()
                .checkTokenAccess("isAuthenticated()")
                .allowFormAuthenticationForClients();
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        // super.configure(clients);
        clients
                .inMemory()
                .withClient("client")
                .secret("{noop}secret")
                // 为true 直接自动授权成功返回CODE
                // .autoApprove(true)
                .redirectUris("http://example.com")
                .scopes("read", "write", "get_user_info", "get_user_me")
                // .accessTokenValiditySeconds(2 * 60)
                // .refreshTokenValiditySeconds(2 * 60)
                .authorizedGrantTypes("authorization_code");


    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        //将oauth confirm 更换为自定义confirm 页面
        endpoints.pathMapping("/oauth/confirm_access", "/auth/confirm_access");
    }
}
